package demos;

import org.junit.Test;

import java.util.concurrent.CompletableFuture;

public class CompletableDemo {
    @Test
    public void runAsyncTest() {
        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "没有返回值处理");
        });
        future.whenComplete((aVoid, throwable) -> {
            System.out.println("finished");
        }).exceptionally(throwable -> {
            throwable.printStackTrace();
            return null;
        });
    }

    @Test
    public void supplyAsyncTest() {
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "\t有返回值类型");
//            int age = 10 / 0;
            return 1024;
        });
        future.whenComplete((integer, throwable) -> {
            System.out.println("integer = " + integer);
            System.out.println("throwable = " + throwable);
        }).exceptionally(throwable -> {
            System.out.println("##### exception = " + throwable.getMessage());
            return -1;
        });
    }
}
